package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class X3BottomMileChangeDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3BaseBottomSheetDialog.IOnHideKey {

    private TextView tv_title;
    private TextView tv_cancel;
    private TextView tv_affirm;
    private EditText et_input;
    private RelativeLayout rl_input;
    private TextView tv_unit;
    private WheelView<Integer> wv_mile;

    private String km;
    private List<Integer> milesList;

    private int newCurrentMiles;
    private int currentMiles;

    public X3BottomMileChangeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_affirm = getView(R.id.tv_affirm);
        tv_cancel = getView(R.id.tv_cancel);
        tv_title = getView(R.id.tv_title);
        et_input = getView(R.id.et_input);
        rl_input = getView(R.id.rl_input);
        tv_unit = getView(R.id.tv_unit);
        wv_mile = getView(R.id.wv_mile);

        km = (String) SpHelper.getData(X3Constent.MILEAGEUNIT, "km");
        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);

        tv_unit.setText(km + "");

        milesList = new ArrayList<>();
        milesList.clear();
        for (int i = 0; i < 10; i++) {
            milesList.add(i * 100);
        }

        for (int i = 1; i < 100; i++) {
            milesList.add(i * 1000);
        }

        for (int i = 10; i <= 100; i++) {
            milesList.add(i * 10000);
        }

        this.setiOnHideKey(this);

        wv_mile.setData(milesList);
        wv_mile.setTypeface(ResourcesCompat.getFont(mContext,R.font.pingfan_bold));
        wv_mile.setOnItemSelectedListener((wheelView, data, position) -> {
            newCurrentMiles = data;
            String s = et_input.getText().toString();
            if ((data + "").equals(s)) {
                return;
            }
            et_input.setText(data + "");
            et_input.setSelection((data + "").length());

        });
    }

    /**
     * 选择保养里程
     * @param currentMiles
     */
    public void showCurrentMiles(int currentMiles,int nextCurrentMiles) {
        if (currentMiles <= 0) {
            currentMiles = 80000;
        }
        this.currentMiles = nextCurrentMiles;

        et_input.setText(currentMiles + "");
        et_input.setSelection((currentMiles + "").length());
        int currentIndex = milesList.indexOf(currentMiles);
        if (currentIndex < 0) {
            milesList.add(currentMiles);
            Collections.sort(milesList);
            currentIndex = milesList.indexOf(currentMiles);
        }
        wv_mile.setData(milesList);
        wv_mile.setSelectedItemPosition(currentIndex);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_mile_change_dialog;
    }

    IOnGetCurrentMile iOnGetCurrentMile;

    public void setiOnGetCurrentMile(IOnGetCurrentMile iOnGetCurrentMile) {
        this.iOnGetCurrentMile = iOnGetCurrentMile;
    }

    public interface IOnGetCurrentMile {
        void iOnGetMile(int miles);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {// 点击确定
            dismiss();
            Integer itemData = wv_mile.getSelectedItemData();
            if (currentMiles == 0) {
                if (iOnGetCurrentMile != null) {
                    iOnGetCurrentMile.iOnGetMile(itemData);
                }
            } else {
//                    if (itemData < currentMiles) {
//                        itemData = currentMiles;
//                    }
                if (iOnGetCurrentMile != null) {
                    iOnGetCurrentMile.iOnGetMile(itemData);
                }
            }
        }
    }

    @Override
    public void iOnHideKeyBoard() {
        String input = et_input.getText().toString();

        if (X3StringUtils.isEmpty(input)) {
            et_input.setText("0");
        }else {
            if (input.equals(wv_mile.getSelectedItemData() + "")) {
                return;
            }
            if (Integer.parseInt(input) > 1000000) {
                et_input.setText("1000000");
                et_input.setSelection("1000000".length());
                input = "1000000";
            }

            int i = milesList.indexOf(Integer.parseInt(input));
            if (i < 0) {
                milesList.add(Integer.parseInt(input));
                Collections.sort(milesList);
                i = milesList.indexOf(Integer.parseInt(input));
            }
            wv_mile.setData(milesList);
            wv_mile.setSelectedItemPosition(i);
        }

    }
}
